package com.mars.manager.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mars.manager.entity.Menu;
import com.mars.manager.exception.MenuBizException;
import com.mars.manager.service.MenuService;
import com.mars.web.common.support.BaseController;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {
	
	@Autowired
	private MenuService service ;
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main() {
		return viewName("main") ;
	}

	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Long id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public Object add(Menu entity) {
		try {
			this.service.insert(entity) ;
		} catch (MenuBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		
		return setResponseResultAddOk();
	}
	
	@RequestMapping(value="/delete/{ids}", method=RequestMethod.GET)
	@ResponseBody
	public Object delete(@PathVariable String ids) {
		try {
			this.service.deleteByIds(ids) ;
		} catch (MenuBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultDeleteOk();
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public Object update(Menu entity) {
		try {
			this.service.update(entity) ;
		} catch (MenuBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultUpdateOk();
	}
	
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	@ResponseBody
	public Menu get(@PathVariable Long id) {
		return this.service.selectById(id) ;
	}
	
	@RequestMapping(value="/getMenuByURL", method=RequestMethod.GET)
	@ResponseBody
	public Menu getMenuByURL(String url) {
		return this.service.getMenuByURL(url);
	}
	
	@RequestMapping(value="/category", method=RequestMethod.GET)
	@ResponseBody
	public List<Menu> category() {
		return this.service.getMenuCategory() ;
	}
	
	@RequestMapping(value="/getChildren/{parent_id}", method=RequestMethod.GET)
	@ResponseBody
	public List<Menu> getChildren(@PathVariable Long parent_id) {
		return this.service.getMenuByParentId(parent_id) ;
	}
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	@ResponseBody
	public List<Menu> list() {
		return this.service.getAllMenus() ;
	}
	
}
